package com.tomvian.base 
{
	import flash.display.DisplayObject;
	/**
	 * Domain
	 * @author Tom Vian
	 */
	public class Domain
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public static const LOCALHOST:uint = 		0;
		public static const SUPERFLASHBROS:uint = 	1;
		public static const NEWGROUNDS:uint = 		2;
		public static const ARMORGAMES:uint = 		3;
		public static const KONGREGATE:uint = 		4;
		public static const OTHER:uint = 			100;
		
		public static var siteLock:Array;
		
		public static var currentDomain:uint;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public static function checkDomain(root:DisplayObject):void
		{
			var address:String = root.loaderInfo.url.toLowerCase();
			
			var domainName:String;
			
			if (address.substr(0, 7) == "file://") 	domainName = "localhost";
			else									domainName = address.split("/")[2];
			
			if (!domainName) 
			{
				currentDomain = OTHER;
				return;
			}
			
				 if (matchDomain(domainName, "localhost")) 			currentDomain = LOCALHOST;
			else if (matchDomain(domainName, "superflashbros.net")) currentDomain = SUPERFLASHBROS;
			else if (matchDomain(domainName, "ungrounded.net")) 	currentDomain = NEWGROUNDS;
			else if (matchDomain(domainName, "armorgames.com")) 	currentDomain = ARMORGAMES;
			else if (matchDomain(domainName, "kongregate.com")) 	currentDomain = KONGREGATE;
			else 													currentDomain = OTHER;
			
			if (siteLock && currentDomain != LOCALHOST)
			{
				var onAllowedSite:Boolean = false;
				
				for (var i:uint = 0, l:uint = siteLock.length; i < l; i++)
				{
					onAllowedSite = onAllowedSite || matchDomain(domainName, siteLock[i]);
				}
				
				if (!onAllowedSite)
				{
					root.scaleX = 100;
					root.y = 2000;
				}
			}
		}
		
		protected static function matchDomain(fullDomainName:String, testDomian:String):Boolean
		{
			return fullDomainName.substr(fullDomainName.length - testDomian.length, testDomian.length) == testDomian;
		}
	}
}